﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YJKStatisticTable.AreaTable.DataManager;

namespace YJKStatisticTable.AreaTable.Forms
{
	public partial class StatisticalSettingForm : System.Windows.Forms.Form
	{
		public List<int> SelectIndex()
		{
			List<int> list = new List<int>();
			foreach (TreeNode treeNode in this.m_FloorNodeList)
			{
				if (treeNode.Checked)
				{
					int item = Convert.ToInt32(treeNode.Tag.ToString());
					list.Add(item);
				}
			}
			return list;
		}

		public StatisticalSettingForm(List<StatisticalSettingForm.AreaViewsInfoByFloor> sortedAreaViewInfos)
		{
			this.m_SortedAreaViewInfos = sortedAreaViewInfos;
			this.InitializeComponent();
			this.treeView.CheckBoxes = true;
			this.treeView.ShowLines = true;
			this.treeView.DrawMode = TreeViewDrawMode.OwnerDrawAll;
			this.treeView.DrawNode += this.treeView_DrawNode;
		}

		private void btnAll_Click(object sender, EventArgs e)
		{
			foreach (TreeNode treeNode in this.m_FloorNodeList)
			{
				if (!(treeNode.BackColor == this.DisabledNodeColor))
				{
					treeNode.Checked = true;
				}
			}
		}

		private void btnReverse_Click(object sender, EventArgs e)
		{
			foreach (TreeNode treeNode in this.m_FloorNodeList)
			{
				if (!(treeNode.BackColor == this.DisabledNodeColor))
				{
					treeNode.Checked = !treeNode.Checked;
				}
			}
		}

		private void btnNone_Click(object sender, EventArgs e)
		{
			foreach (TreeNode treeNode in this.m_FloorNodeList)
			{
				if (!(treeNode.BackColor == this.DisabledNodeColor))
				{
					treeNode.Checked = false;
				}
			}
		}

		private void StatisticalSettingForm_Load(object sender, EventArgs e)
		{
			int num = 0;
			foreach (StatisticalSettingForm.AreaViewsInfoByFloor areaViewsInfoByFloor in this.m_SortedAreaViewInfos)
			{
				string floorName = areaViewsInfoByFloor.m_FloorName;
				TreeNode treeNode = new TreeNode(floorName);
				this.m_FloorNodeList.Add(treeNode);
				this.treeView.Nodes.Add(treeNode);
				treeNode.Tag = num;
				if (areaViewsInfoByFloor.m_IsAvailableStatistics)
				{
					treeNode.BackColor = this.EnabledNodeColor;
					treeNode.Checked = true;
				}
				else
				{
					treeNode.BackColor = this.DisabledNodeColor;
					treeNode.Checked = false;
				}
				num++;
				foreach (StatisticalSettingForm.AreaViewInfo areaViewInfo in areaViewsInfoByFloor.m_AreaViewInfoList)
				{
					TreeNode node = new TreeNode(areaViewInfo.m_AreaViewName);
					treeNode.Nodes.Add(node);
				}
				treeNode.Expand();
			}
		}

		private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
		{
			if (this.DisabledNodeColor == e.Node.BackColor)
			{
				e.Cancel = true;
			}
		}

		private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
		{
		}

		private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			if (e.Node.Level > 0)
			{
				this.HideCheckBox(this.treeView, e.Node);
			}
			else if (this.DisabledNodeColor == e.Node.BackColor)
			{
				this.HideCheckBox(this.treeView, e.Node);
			}
			e.DrawDefault = true;
		}

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref StatisticalSettingForm.TVITEM lParam);

		private void HideCheckBox(TreeView tvw, TreeNode node)
		{
			StatisticalSettingForm.TVITEM tvitem = default(StatisticalSettingForm.TVITEM);
			tvitem.hItem = node.Handle;
			tvitem.mask = 8;
			tvitem.stateMask = 61440;
			tvitem.state = 0;
			StatisticalSettingForm.SendMessage(tvw.Handle, 4415, IntPtr.Zero, ref tvitem);
		}

		private const int TVIF_STATE = 8;

		private const int TVIS_STATEIMAGEMASK = 61440;

		private const int TV_FIRST = 4352;

		private const int TVM_SETITEM = 4415;

		private List<StatisticalSettingForm.AreaViewsInfoByFloor> m_SortedAreaViewInfos;

		private List<TreeNode> m_FloorNodeList = new List<TreeNode>();

		private Color EnabledNodeColor = Color.White;

		private Color DisabledNodeColor = Color.LightGray;

		public struct AreaViewInfo
		{
			public AreaViewInfo(string areaViewName, AreaType areaType)
			{
				this.m_AreaViewName = areaViewName;
				this.m_AreaType = areaType;
			}

			public string m_AreaViewName;

			public AreaType m_AreaType;
		}

		public struct AreaViewsInfoByFloor
		{
			public AreaViewsInfoByFloor(string floorName, bool isAvailableStatistics, List<StatisticalSettingForm.AreaViewInfo> areaViewInfoList)
			{
				this.m_FloorName = floorName;
				this.m_IsAvailableStatistics = isAvailableStatistics;
				this.m_AreaViewInfoList = areaViewInfoList;
			}

			public string m_FloorName;

			public bool m_IsAvailableStatistics;

			public List<StatisticalSettingForm.AreaViewInfo> m_AreaViewInfoList;
		}

		[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 8)]
		private struct TVITEM
		{
			public int mask;

			public IntPtr hItem;

			public int state;

			public int stateMask;

			[MarshalAs(UnmanagedType.LPTStr)]
			public string lpszText;

			public int cchTextMax;

			public int iImage;

			public int iSelectedImage;

			public int cChildren;

			public IntPtr lParam;
		}
	}
}
